/**
 * 
 */
package com.expense.util;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.List;

import org.apache.log4j.Logger;

import jxl.Workbook;
import jxl.write.DateFormat;
import jxl.write.DateTime;
import jxl.write.Label;
import jxl.write.Number;
import jxl.write.WritableCellFormat;
import jxl.write.WritableFont;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import jxl.write.WriteException;
import jxl.write.biff.RowsExceededException;


/**
 * 根据数据创建Excel
 * @author xj
 *
 */
public class CreatExcel {
	public Logger logger = Logger.getLogger(this.getClass());
	
	/******创建Excel
	 * @param 
	 * fileName 文件名,sheetNames sheet名称数组,lists 数据List数组
	 * dataTypeStrings 数据类型数组
	 * *******/
	public static void creatExcelFile(String[] sheetNames,List[] lists,String filePath){
			FileOutputStream fileOut=null;
			 try {
				 fileOut = new FileOutputStream(filePath);
			} catch (FileNotFoundException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
		  try {
			WritableWorkbook workbook = Workbook.createWorkbook(fileOut);
			WritableSheet sheet=null;
			
			
			WritableFont wf = new WritableFont(WritableFont.TIMES, 15, WritableFont.BOLD, false); 
			WritableCellFormat titleFormat=new WritableCellFormat(wf);

			
			//这个显示当前时间的所有信息，包括年月日小时分秒 
			SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
			DateFormat df = new DateFormat("yyyy-MM-dd hh:mm:ss"); 
			WritableCellFormat datefmt = new WritableCellFormat(df); 

			Number labelNF=null;
			Label label=null;
			DateTime labelDTF=null;
			try {
				for (int i = 0; i < sheetNames.length; i++) { 
						sheet= workbook.createSheet(sheetNames[i], 0);
						String [] strings=null;
						for (int h = 0; h < lists[i].size(); h++) {
							strings=(String[]) lists[i].get(h);
							    
								for (int j = 0; j < strings.length; j++) {
									try {
										if (h==0) {
											label = new  Label(j,h,strings[j],titleFormat);
											//label = new  Label(j,h,strings[j]);
											sheet.addCell(label);
										}else {	
											 
												label = new  Label(j,h,strings[j]);
												sheet.addCell(label);
											 
										}
									} catch (Exception e) {
										// TODO: handle exception
										label = new  Label(j,h,strings[j]);
										sheet.addCell(label);
									}
									
									 
								}
							 
							
						}
				}
				workbook.write();
			}catch (Exception e) {
				// TODO: handle exception
			}
			workbook.close();
			fileOut.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} 
		catch (RowsExceededException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (WriteException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		 
	}
}
